<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class people_controller extends admin_controller
{
    // Get list
    public function __construct()
    {
        parent::__construct();
        $this->page_title = 'Thông tin';
        $this->table_name = 'people';
        $this->route_name = 'people';
        $this->business_model = $this->people_model;
    }

    public function get_post_data()
    {

        $image_path = $this->upload_image('new_file',$this->input->post('image_path'));
        $thumbnail_path = $this->upload_image('new_thumbnail',$this->input->post('thumbnail_path'));

        $post_data = array(
            'group_id' => $this->input->post('group_id'),
            'name_en' => $this->input->post('name_en'),
            'name_vi' => $this->input->post('name_vi'),
            'position_en' => $this->input->post('position_en'),
            'position_vi' => $this->input->post('position_vi'),
            'description_en' => $this->input->post('description_en'),
            'description_vi' => $this->input->post('description_vi'),
            'image_path' => $image_path,
            'thumbnail_path' => $thumbnail_path,
            'sort_order' => $this->input->post('sort_order'),
            'modified_date' => time()
        );
        return $post_data;
    }

    public function get_list()
    {
        $list = array();
        $news = $this->business_model->get_elements();
        foreach($news as $item)
        {
            $actions = array();
            $actions[] = array(
                'text' => 'Sửa',
                'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/'.$item->id
            );

            $list[] = array(
                'can_delete' => $item->id,
                'group_id' => $this->people_group_model->get_element_by_id($item->group_id)->name_en,
                'name_en' => $item->name_en,
                'name_vi' => $item->name_vi,
                'position_en' => $item->position_en,
                'position_vi' => $item->position_vi,
                'image_path' => $item->image_path,
                'sort_order' => $item->sort_order,
                'actions' => $actions
            );
        }
        return $list;
    }

    public function get_header_list()
    {
        $list = array();
        $list[] = 'Tên nhóm';
        $list[] = 'Tên (Anh)';
        $list[] = 'Tên (Việt)';
        $list[] = 'Chức vụ (Anh)';
        $list[] = 'Chức vụ (Việt)';
        $list[] = 'Đường dẫn hình';
        $list[] = 'Thứ tự';
        $list[] = 'Công việc';
        return $list;
    }

    public function get_fields($id)
    {
        if($id == -1)
        {
            $model->group_id = '';
            $model->name_en = '';
            $model->name_vi = '';
            $model->position_en = '';
            $model->position_vi = '';
            $model->description_en = '';
            $model->description_vi = '';
            $model->image_path = '';
            $model->thumbnail_path = '';
            $model->sort_order = '';
        }
        else
        {
            $model = $this->business_model->get_element_by_id($id);
        }
        $fields = array();
        $fields[] = array(
            'text' => 'Tên nhóm',
            'name' => 'group_id',
            'value' =>$model->group_id,
            'options' => array(
                $this->array_to_object(array(
                    'text' => 'Management Board',
                    'value' => '1'
                )),
                $this->array_to_object(array(
                    'text' => 'Business development',
                    'value' => '2'
                )),
                $this->array_to_object(array(
                    'text' => 'Sales and marketing',
                    'value' => '3'
                )),
                $this->array_to_object(array(
                    'text' => 'Finance and accounting',
                    'value' => '4'
                )),
                $this->array_to_object(array(
                    'text' => 'Legal',
                    'value' => '5'
                ))
            ),
            'type' => 'select',
            'select_display' => 'text',
            'select_value' => 'value',
            'select_selected' => $model->group_id
        );
        $fields[] = array(
            'text' => 'Tên (Anh)',
            'name' => 'name_en',
            'value' => $model->name_en,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Tên (Việt)',
            'name' => 'name_vi',
            'value' => $model->name_vi,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Chức vụ (Anh)',
            'name' => 'position_en',
            'value' => $model->position_en,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Chức vụ (Việt)',
            'name' => 'position_vi',
            'value' => $model->position_vi,
            'type' => 'text'
        );
        $fields[] = array(
            'text' => 'Mô tả (Anh)',
            'name' => 'description_en',
            'value' => $model->description_en,
            'type' => 'textarea'
        );
        $fields[] = array(
            'text' => 'Mô tả (Việt)',
            'name' => 'description_vi',
            'value' => $model->description_vi,
            'type' => 'textarea'
        );
        $fields[] = array(
            'text' => '',
            'name' => 'image_path',
            'value' => $model->image_path,
            'type' => 'hidden'
        );
        $fields[] = array(
            'text' => '',
            'name' => '',
            'value' => '<img src="'.base_url().$model->image_path.'" width="220px" height="220px">',
            'type' => 'label'
        );
        $fields[] = array(
            'text' => 'Tải hình khác',
            'name' => 'new_file',
            'value' => '',
            'type' => 'file'
        );

        $fields[] = array(
            'text' => '',
            'name' => 'thumbnail_path',
            'value' => $model->thumbnail_path,
            'type' => 'hidden'
        );
        $fields[] = array(
            'text' => '',
            'name' => '',
            'value' => '<img src="'.base_url().$model->thumbnail_path.'" width="100px" height="100px">',
            'type' => 'label'
        );
        $fields[] = array(
            'text' => 'Tải thumbnail khác',
            'name' => 'new_thumbnail',
            'value' => '',
            'type' => 'file'
        );

        $fields[] = array(
            'text' => 'Thứ tự',
            'name' => 'sort_order',
            'value' => $model->sort_order,
            'type' => 'text'
        );

        return $fields;
    }
}